package no.ntnu.item.ttm4115.termproject.configurationagent.configuration;


import java.io.File;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


import no.ntnu.item.ttm4115.termproject.extrasagent.Group;
import no.ntnu.item.ttm4115.termproject.extrasagent.Player;
import no.tellu.common.actorframe.ActorSM;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class LoadConfiguration {

	private ActorSM asm;
	private Vector<Group> groups;
	private Vector<Player> players;

	
	public LoadConfiguration(ActorSM asm){
		super();
		this.asm = asm;
		this.players = loadPlayersFromXMLFile();
		this.groups = loadGroupsFromXMLFile();
		
	}

	
	public Vector<Group> loadGroupsFromXMLFile() {
		groups = new Vector<Group>();
		try {

			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse (new File("c:/ttm4115config/groupConfig.xml"));

			// normalize text representation
			doc.getDocumentElement ().normalize ();
			//System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());


			NodeList listOfGroups = doc.getElementsByTagName("group");
			//int totalGroups = listOfGroups.getLength();
			//System.out.println("Total no of groups : " + totalGroups);

			for(int s=0; s<listOfGroups.getLength() ; s++){

				Node firstGroupNode = listOfGroups.item(s);
				if(firstGroupNode.getNodeType() == Node.ELEMENT_NODE){
					
					Group group = new Group();
					
					Element firstGroupElement = (Element)firstGroupNode;

					//-------
					NodeList groupNameList = firstGroupElement.getElementsByTagName("id");
					Element groupNameElement = (Element)groupNameList.item(0);

					NodeList groupTNList = groupNameElement.getChildNodes();
					String groupId = ((Node)groupTNList.item(0)).getNodeValue().trim();
					//System.out.println("Loading Group Name : " + groupName);
					
					group.setGroupName(groupId);
					//group.setId(s);
					
					//load in all roles for a group
					NodeList listOfRoles = firstGroupElement.getElementsByTagName("role"); 
					for(int r=0; r<listOfRoles.getLength(); r++){
						Node firstRoleNode = listOfRoles.item(r);
						if(firstRoleNode.getNodeType()== Node.ELEMENT_NODE){
							Element groupRoleElement = (Element)firstRoleNode;
							NodeList roleList = groupRoleElement.getChildNodes();
							String rolle =  ((Node)roleList.item(0)).getNodeValue().trim();		
							if(rolle.equals("GroupChat")){
								group.setChat(true);
							}else if(rolle.equals("DiscussionForum")){
								group.setDiscussion(true);
							}else if(rolle.equals("GroupIm")){
								group.setIm(true);
							}
						}
					}
					groups.add(group);
				}
			}
		}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());

		}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		}catch (Throwable t) {
			t.printStackTrace ();
		}
		//System.exit (0);

		return groups;
	}		

	public Vector<Player> loadPlayersFromXMLFile() {
		players = new Vector<Player>();
		try {

			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse (new File("c:/ttm4115config/playerConfig.xml"));

			// normalize text representation
			doc.getDocumentElement ().normalize ();	
			
			//------ Loading the players
					
			NodeList listOfPlayers = doc.getElementsByTagName("player");
			//int totalPlayers = listOfPlayers.getLength();
			//System.out.println("Total no of Players : " + totalPlayers);

			
					
			for(int p=0; p<listOfPlayers.getLength(); p++){
						
				Player player = new Player();
				Node firstPlayerNode = listOfPlayers.item(p);
						
				if(firstPlayerNode.getNodeType() == Node.ELEMENT_NODE){
				//get player id
					Element firstPlayerElement = (Element)firstPlayerNode;
						
					NodeList playerIdList = firstPlayerElement.getElementsByTagName("id");
					Element playerIdElement = (Element)playerIdList.item(0);
					
					NodeList textList = playerIdElement.getChildNodes();
					String usernameText = ((Node)textList.item(0)).getNodeValue().trim();
						
					player.setUserName(usernameText);
						
					//get player password
					NodeList playerPasswordList = firstPlayerElement.getElementsByTagName("password");
					Element playerPasswordElement = (Element)playerPasswordList.item(0);

					NodeList passwordList = playerPasswordElement.getChildNodes();
					String playerPassword = ((Node)passwordList.item(0)).getNodeValue().trim();
					//System.out.println("Loading password : " + playerPassword);
						
					player.setPassword(playerPassword);
					//get player roles
					NodeList playerListOfRoles = doc.getElementsByTagName("playerrole");
					System.out.println(playerListOfRoles.getLength());							
						
					for (int t = 0; t < playerListOfRoles.getLength(); t++) {
						Node firstRoleNode = playerListOfRoles.item(t);
						if(firstRoleNode.getNodeType()== Node.ELEMENT_NODE){
							Element playerRoleElement = (Element)firstRoleNode;
							NodeList roleList = playerRoleElement.getChildNodes();
							String rolle =  ((Node)roleList.item(0)).getNodeValue().trim();						
							if(rolle.equals("PlayerChat")){
								player.setChat(true);
							}else if(rolle.equals("DiscussionForum")){
								player.setDiscussion(true);
							}else if(rolle.equals("PlayerIm")){
								player.setIm(true);
							}else if(rolle.equals("LocationAwareness")){
								player.setLocationAwareness(true);
							}else if(rolle.equals("CommunityAwareness")){
								player.setCommunityAwareness(true);
							}
						}
					}
					
					players.add(player);
					
				}
				//end of if
					
			}	
			//end of player iteration
					
			
			
		}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());

		}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		}catch (Throwable t) {
			t.printStackTrace ();
		}
		//System.exit (0);

		return players;
	}
	public Vector<Group> getGroups() {
		return groups;
	}

	public void setGroups(Vector<Group> groups) {
		this.groups = groups;
	}
	public Vector<Player> getPlayers(){
		return players;
	}
	public void setPlayers(Vector<Player> players){
		this.players = players;
	}

	public ActorSM getAsm() {
		return asm;
	}

	public void setAsm(ActorSM asm) {
		this.asm = asm;
	}


}
